package app.controller;

import app.config.DisqueConfig;
import biz.paluch.spinach.DisqueClient;
import biz.paluch.spinach.DisqueURI;
import biz.paluch.spinach.api.AddJobArgs;
import biz.paluch.spinach.api.sync.DisqueCommands;
import com.github.xetorthio.jedisque.Jedisque;
import com.github.xetorthio.jedisque.JobParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author faith.huan 2018-09-02 13:11
 */
@RestController
@RequestMapping("/testDisque")
@Slf4j
public class TestDisqueController {

//    @Autowired
//    DisqueCommands commands;

    @PostMapping("/add")
    public Map<String, Object> add(@RequestParam("queueName") String queueName,
                                   @RequestParam("jobContent") String jobContent,
                                   @RequestParam("delay") int delay) throws URISyntaxException {

        Jedisque jedisque = new Jedisque(
                // 85
                new URI("disque://47.105.39.155:50711")//,
//                // 88
//                new URI("disque://47.104.89.100:50711"),
                // 83
                //new URI("disque://47.104.254.218:50711")
        );

        String jobId = "";
        Map<String, Object> resultMap = new HashMap<>();

        JobParams params = new JobParams();
        params.setDelay(delay);
        jobId = jedisque.addJob(queueName, jobContent, 0, params);


        /*
        // spinach client
        AddJobArgs addJobArgs =new AddJobArgs();
        addJobArgs.setDelay(delay);
        String jobId = commands.addjob(queueName, jobContent, 1, TimeUnit.SECONDS,addJobArgs);
        */
        log.info("add");
        resultMap.put("jobId", jobId);

        return resultMap;
    }

}
